#pragma once

#include <iostream>
#include <memory>
#include <string>
#include <map>
#include <memory>
#include <thread>
#include <grpc/compression.h>
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <grpc/support/time.h>
#include <grpc++/security/auth_context.h>
#include <grpc++/support/config.h>
#include <grpc++/support/status.h>
#include <grpc++/support/string_ref.h>
#include <grpc++/support/time.h>
#include <grpc++/security/auth_context.h>
#include <grpc++/security/credentials.h>
#include <grpc++/grpc++.h>

#include "qqmessage.grpc.pb.h"
#include "MessageEngineConfig.h"

namespace kq{

	class MessageEngine //: public std::enable_shared_from_this<MessageEngine>
	{
	public:
		MessageEngine(MessageEngineConfig messageEngineConfig, int authCode, int64_t qqId,const char* qqName);
		virtual ~MessageEngine(void);
		
		void start();
		void stop();
		bool eventPrivateMsg(int32_t subType, int32_t sendTime, int64_t fromQQ, const char *msg, int32_t font);
		bool eventGroupMsg(int32_t subType, int32_t sendTime, int64_t fromGroup, int64_t fromQQ, const char *fromAnonymous, const char *msg, int32_t font);
		bool eventDiscussMsg(int32_t subType, int32_t sendTime, int64_t fromDiscuss, int64_t fromQQ, const char *msg, int32_t font);
		int authCode;
		int64_t qqId;
		std::string qqName;
		MessageEngineConfig messageEngineConfig;
	private:
		void emulatorServerLoop();
		std::unique_ptr<qq::message::Controller::Stub> controllerClient_;
		std::shared_ptr<std::thread> emulatorServerThread_;
		std::unique_ptr<grpc::Server> emulatorServer_;

	};


}

